package club.caihappy.store.service.impl;

import club.caihappy.store.entity.Cart;
import club.caihappy.store.entity.OrderItem;
import club.caihappy.store.entity.Product;
import club.caihappy.store.mapper.CartMapper;
import club.caihappy.store.mapper.ProductMapper;
import club.caihappy.store.service.ICartService;
import club.caihappy.store.service.IProductService;
import club.caihappy.store.service.ex.*;
import club.caihappy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by DELL on 2022/3/22
 **/
@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private IProductService productService;


    @Override
    public void addToCart(Integer pid, Integer uid, Integer num, String username) {
        //判断当前商品是否存在于当前用户的购物车中
        Cart cart = cartMapper.findCart(uid, pid);
        Date date = new Date();
        if (cart != null) {
            Integer rows = cartMapper.updateNumByCid(cart.getCid(), cart.getNum() + num, username, date);
            if (rows != 1) {
                throw new UpdateException("更新信息时出现未知错误");
            }
        } else {
            Cart res = new Cart();
            res.setUid(uid);
            res.setPid(pid);
            res.setNum(num);
            res.setPrice(productService.getProduct(pid).getPrice());
            res.setCreatedUser(username);
            res.setCreatedTime(date);
            res.setModifiedUser(username);
            res.setModifiedTime(date);

            Integer rows = cartMapper.insert(res);
            if (rows != 1) {
                throw new InsertException("添加信息时出现未知错误");
            }
        }

    }

    @Override
    public List<CartVO> getVOByUid(Integer uid) {
        return cartMapper.findVOByUid(uid);
    }

    @Override
    public void changeNum(Integer cid, Integer uid, String username, Integer num) {
        Cart cart = cartMapper.findByCid(cid);
        if (cart == null) {
            throw new CartNotFoundException("购物车数据不存在");
        }
        if (!cart.getUid().equals(uid)) {
            throw new AccessDeniedException("数据非法访问");
        }
        //如果商品数量为0时删除该商品记录
        if (num <= 0) {
            Integer rows = cartMapper.deleteCart(cid);
            if (rows != 1) {
                throw new DeleteException("删除数据失败");
            }
        } else {
            Integer rows = cartMapper.updateNumByCid(cid, num, username, new Date());
            if (rows != 1) {
                throw new UpdateException("更新数据失败");
            }
        }

    }

    @Override
    public void deleteCart(Integer cid, Integer uid) {
        Cart cart = cartMapper.findByCid(cid);
        if (cart == null) {
            throw new CartNotFoundException("购物车数据不存在");
        }
        if (!cart.getUid().equals(uid)) {
            throw new AccessDeniedException("数据非法访问");
        }
        Integer rows = cartMapper.deleteCart(cid);
        if (rows != 1) {
            throw new DeleteException("删除数据失败");
        }
    }

    @Override
    public List<CartVO> getVOByCid(Integer uid, Integer[] cids) {
        if (cids == null || cids.length == 0) {
            throw new CartNotFoundException("没有购物车数据");
        }
        List<CartVO> carts = cartMapper.findVOByCid(cids);
        Iterator<CartVO> iterator = carts.iterator();
        while (iterator.hasNext()) {
            CartVO cartVO = iterator.next();
            if (!cartVO.getUid().equals(uid)) {//表示当前的数据不属于当前的用户
                //从集合中移除当前的元素
               iterator.remove();
            }
        }
        return carts;
    }
}
